package com.alex.seo.service.campaign;

import java.util.HashSet;
import java.util.Set;

import com.alex.seo.dao.group.SiteGroupDao;
import com.alex.seo.dao.site.SiteDao;
import com.alex.seo.domain.campaign.Campaign;
import com.alex.seo.domain.campaign.PublishingCampaign;
import com.alex.seo.domain.site.Site;
import com.alex.seo.service.campaign.dto.CampaignDTO;
import com.alex.seo.service.campaign.dto.PublishingCampaignDTO;

public abstract class CampaignConverter {

	public static Campaign convert(CampaignDTO campaignDTO, SiteDao siteDao, SiteGroupDao siteGroupDao) {
		if (campaignDTO instanceof PublishingCampaignDTO) {
			PublishingCampaignDTO publishingCampaignDTO = (PublishingCampaignDTO) campaignDTO;
			Set<Site> sites = new HashSet<Site>();
			if (publishingCampaignDTO.getSiteIds() != null) {
				sites.addAll(siteDao.getAll(publishingCampaignDTO.getSiteIds(), false));
			}
			if (publishingCampaignDTO.getSiteGroupIds() != null) {
				sites.addAll(siteGroupDao.getSitesOfGroups(publishingCampaignDTO.getSiteGroupIds()));
			}
			
			return new PublishingCampaign(
					publishingCampaignDTO.getName(),
					sites,
					publishingCampaignDTO.getJobScheduler(),
					publishingCampaignDTO.getContentSource());
		} else {
			throw new IllegalArgumentException("Unknown campaign type: " + campaignDTO.getClass().getSimpleName());
		}
	}
	
}
